﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class MatHangAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
                LoadMaSothue();
                LoadLoaiHang();
                LoadDonVitinh();
                LoadKho();
            }

        }
        public void LoadHienthi()
        {

            IUnitOfWork iunitfwork = new EFUnitOfWork();
            string strid = "" + Request["MAMH"];
            MATHANGRepository repository = new MATHANGRepository(new EFRepository<MATHANG>(), iunitfwork);
            MATHANG objMatHang = null;
            if (!string.IsNullOrEmpty(strid))
            {
                objMatHang = repository.GetById(strid);
                txtMamh.Value = objMatHang.MAMH;
                txttenmh.Value = objMatHang.TENMH;
                //txtsoluong.Value = objMatHang.SOLUONGMH.ToString();
                //txtgianhap.Value = objMatHang.GIAMUA.ToString();
                //txtgiaban.Value = objMatHang.GIABAN.ToString();
                Dlthue.SelectedValue = objMatHang.MATH;
                Dlloaihang.SelectedValue = objMatHang.MANH;
                Dldonvitinh.SelectedValue = objMatHang.MADVT;
                txtmota.Value = objMatHang.MOTA;
                Dlkho.SelectedValue = objMatHang.KHO.MAKHO;
                if (objMatHang.TINHTRANG == true)
                {
                    rddangban.Checked = true;
                }
                else
                {
                    rdkoban.Checked = false;
                }
            }

        }
        /// <summary>
        /// Load Đơn vị tính
        /// </summary>
        public void LoadDonVitinh()
        {

            IUnitOfWork iunitfwork = new EFUnitOfWork();
            DONVITINHRepository repository = new DONVITINHRepository(new EFRepository<DONVITINH>(), iunitfwork);
            IQueryable<DONVITINH> objDonvitinh = repository.All();
            Dldonvitinh.DataSource = objDonvitinh;
            Dldonvitinh.DataValueField = "MADVT";
            Dldonvitinh.DataTextField = "DONVITINH1";
            Dldonvitinh.DataBind();
            Dldonvitinh.Items.Insert(0, new ListItem { Text = "---Chọn đơn vị tính---", Value = "0" });

        }
        /// <summary>
        /// Load mã thuế
        /// </summary>
        public void LoadMaSothue()
        {

            IUnitOfWork iunitfwork = new EFUnitOfWork();
            THUERepository repository = new THUERepository(new EFRepository<THUE>(), iunitfwork);
            IQueryable<THUE> Objthue = repository.All();
            Dlthue.DataSource = Objthue;
            Dlthue.DataValueField = "MATH";
            Dlthue.DataTextField = "SOTHUE";
            Dlthue.DataBind();
            Dlthue.Items.Insert(0, new ListItem { Text = "---Chọn mã thuế---", Value = "0" });
        }
        /// <summary>
        /// Load Loại hàng
        /// </summary>
        public void LoadLoaiHang()
        {

            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHOMHANGRepository repository = new NHOMHANGRepository(new EFRepository<NHOMHANG>(), iunitfwork);
            IQueryable<NHOMHANG> objNhomHang = repository.All();
            Dlloaihang.DataSource = objNhomHang;
            Dlloaihang.DataValueField = "MANH";
            Dlloaihang.DataTextField = "TENNHOMHANG";
            Dlloaihang.DataBind();
            Dlloaihang.Items.Insert(0, new ListItem { Text = "---Chọn loại hàng---", Value = "0" });
        }
        public void LoadKho()
        {
            IUnitOfWork iunfwork = new EFUnitOfWork();
            KHORepository repository = new KHORepository(new EFRepository<KHO>(), iunfwork);
            IQueryable<KHO> ObjKho = repository.All();
            Dlkho.DataSource = ObjKho;
            Dlkho.DataValueField = "MAKHO";
            Dlkho.DataTextField = "TENKHO";
            Dlkho.DataBind();
            Dlkho.Items.Insert(0, new ListItem { Text="---Chọn kho---",Value="0" });
          
        }
        public void capnhap()
        {
            capnhap();
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {

            IUnitOfWork iunitfwork = new EFUnitOfWork();
            string strid = "" + Request["MAMH"];
            MATHANGRepository repository = new MATHANGRepository(new EFRepository<MATHANG>(), iunitfwork);
            MATHANG objMatHang = null;
            bool iscapnhap = true;
            if (!string.IsNullOrEmpty(strid))
            {
                objMatHang = repository.GetById(strid);
                iscapnhap = false;
            }
            else
            {
                objMatHang = new MATHANG();
                iscapnhap = true;
            }
            if (string.IsNullOrEmpty(txtMamh.Value) || Dldonvitinh.SelectedValue.Equals("0") || Dlloaihang.SelectedValue.Equals("0") || Dlthue.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập đầy đủ')</script>");
                return;
            }
            objMatHang.MAMH = txtMamh.Value;
            objMatHang.TENMH = txttenmh.Value;
            //objMatHang.SOLUONGMH = int.Parse(txtsoluong.Value);
            //objMatHang.GIABAN = int.Parse(txtgiaban.Value);
            //objMatHang.GIAMUA = int.Parse(txtgianhap.Value);
            objMatHang.MATH = Dlthue.SelectedValue;
            objMatHang.MANH = Dlloaihang.SelectedValue;
            objMatHang.MADVT = Dldonvitinh.SelectedValue;
            objMatHang.MOTA = txtmota.Value;
            //objMatHang.MAKHO = Dlkho.SelectedValue;
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objMatHang);
                }
                repository.Save();
            }
            catch (Exception ex)
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập đầy đủ')</script>");
                Response.Redirect("MatHangAdd.aspx?MAMH=" + strid);
            }


        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            txtMamh.Value = "";
            txtmota.Value = "";
            txttenmh.Value = "";
            Dldonvitinh.SelectedValue = "0";
            Dlkho.SelectedValue = "0";
            Dlloaihang.SelectedValue = "0";
            Dlthue.SelectedValue = "";
            Response.Redirect("MatHangAdd.aspx");
        }
    }
}